/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * HipNetDevice.cpp - the Hideously Inadequate Protocol 
 *
 * Copyright (C) 2012 Hippy
 * Copyright (C) 2005-2006  Simon Newton
 *
 *
 */

#include <sstream>
#include <string>
#include "ola/Logging.h"
#include "ola/network/NetworkUtils.h"
#include "olad/Plugin.h"
#include "olad/PluginAdaptor.h"
#include "olad/Preferences.h"

#include "plugins/hipnet/HipNetDevice.h"
#include "plugins/hipnet/HipNetNode.h"
#include "plugins/hipnet/HipNetPluginCommon.h"
#include "plugins/hipnet/HipNetPort.h"

namespace ola {
namespace plugin {
namespace hipnet {

const char HipNetDevice::HIPNET_DEVICE_NAME[] = "XLR";

/*
 * Create a new device
 */
HipNetDevice::HipNetDevice(Plugin *owner,
                           Preferences *prefs,
                           PluginAdaptor *plugin_adaptor):
  Device(owner, HIPNET_DEVICE_NAME),
  m_preferences(prefs),
  m_plugin_adaptor(plugin_adaptor),
  m_node(NULL) {
}


/*
 * Start this device
 */
bool HipNetDevice::StartHook() {
  m_node = new HipNetNode(m_plugin_adaptor);

  if (!m_node->Start()) {
    delete m_node;
    m_node = NULL;
    return false;
  }

  stringstream str;
  str << HIPNET_DEVICE_NAME;
  SetName(str.str());

  for (unsigned int i = 0; i < PORTS_PER_DEVICE; i++) {
    HipNetInputPort *input_port = new HipNetInputPort(
        this,
        i,
        m_plugin_adaptor,
        m_node);
    AddPort(input_port);
    
    HipNetOutputPort *output_port = new HipNetOutputPort(this, i, m_node);
    AddPort(output_port);
  }

  return true;
}


/*
 * Stop this device
 */
void HipNetDevice::PrePortStop() {
}

/*
 * Stop this device
 */
void HipNetDevice::PostPortStop() {
  m_node->Stop();
  delete m_node;
  m_node = NULL;
}
}  // hipnet
}  // plugin
}  // ola
